//自适应高度的框架元素
function SetWinHeight(obj) {
	var win = obj;
	if (document.getElementById) {
		if (win && !window.opera) {
			if (win.contentDocument && win.contentDocument.body.offsetHeight)
				win.height = win.contentDocument.body.offsetHeight;
			else if (win.Document && win.Document.body.scrollHeight)
				win.height = win.Document.body.scrollHeight;
		}
	}
}

function preview() {
	var win = document.getElementById('win');
	var frame = window.frames['win'].document.body;
	var text = document.getElementById('post');
	//获得文本区域的值
	md_content = text.value;
	var reg = new RegExp("\r\n", "g");
	md_content = md_content.replace(reg, "\n");
	html_content = markdown.toHTML(md_content);
	//预览并适应窗口高度
	frame.innerHTML = html_content;
	SetWinHeight(win);
}

window.onload = function post() {
	var press = document.getElementById('press');

	press.onclick = function() {
		var html = document.getElementById('post-html');
		var text = document.getElementById('post');
		var form = document.getElementsByTagName('form')[0];

		md_content = text.value;
		var reg = new RegExp("\r\n", "g");
		md_content = md_content.replace(reg, "\n");
		html_content = markdown.toHTML(md_content);
		html.innerHTML = html_content;

		form.submit();
	}
}

//textarea支持tab
HTMLTextAreaElement.prototype.getCaretPosition = function() { //return the caret position of the textarea
	return this.selectionStart;
};
HTMLTextAreaElement.prototype.setCaretPosition = function(position) { //change the caret position of the textarea
	this.selectionStart = position;
	this.selectionEnd = position;
	this.focus();
};
HTMLTextAreaElement.prototype.hasSelection = function() { //if the textarea has selection then return true
	if (this.selectionStart == this.selectionEnd) {
		return false;
	} else {
		return true;
	}
};
HTMLTextAreaElement.prototype.getSelectedText = function() { //return the selection text
	return this.value.substring(this.selectionStart, this.selectionEnd);
};
HTMLTextAreaElement.prototype.setSelection = function(start, end) { //change the selection area of the textarea
	this.selectionStart = start;
	this.selectionEnd = end;
	this.focus();
};

var textarea = document.getElementsByTagName('textarea')[0];

textarea.onkeydown = function(event) {

	//support tab on textarea
	if (event.keyCode == 9) { //tab was pressed
		var newCaretPosition;
		newCaretPosition = textarea.getCaretPosition() + "    ".length;
		textarea.value = textarea.value.substring(0, textarea.getCaretPosition()) + "    " + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);
		textarea.setCaretPosition(newCaretPosition);
		return false;
	}
	if (event.keyCode == 8) { //backspace
		if (textarea.value.substring(textarea.getCaretPosition() - 4, textarea.getCaretPosition()) == "    ") { //it's a tab space
			var newCaretPosition;
			newCaretPosition = textarea.getCaretPosition() - 3;
			textarea.value = textarea.value.substring(0, textarea.getCaretPosition() - 3) + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);
			textarea.setCaretPosition(newCaretPosition);
		}
	}
	if (event.keyCode == 37) { //left arrow
		var newCaretPosition;
		if (textarea.value.substring(textarea.getCaretPosition() - 4, textarea.getCaretPosition()) == "    ") { //it's a tab space
			newCaretPosition = textarea.getCaretPosition() - 3;
			textarea.setCaretPosition(newCaretPosition);
		}
	}
	if (event.keyCode == 39) { //right arrow
		var newCaretPosition;
		if (textarea.value.substring(textarea.getCaretPosition() + 4, textarea.getCaretPosition()) == "    ") { //it's a tab space
			newCaretPosition = textarea.getCaretPosition() + 3;
			textarea.setCaretPosition(newCaretPosition);
		}
	}
}